package com.platform.netty.tcp.task;

import com.platform.common.utils.redis.RedisUtil;
import com.platform.common.utils.spring.SpringUtils;
import com.platform.module.domain.EqAlarm;
import com.platform.module.service.IEqAlarmService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;

@Data
@Slf4j
public class AlarmPoolTask {

    public static TimerTask saveAlarm(final String eqId, final String eqType, final String alarmType, final String alarmMethod, final String alarmMsg)
    {
        final IEqAlarmService eqAlarmService = SpringUtils.getBean(IEqAlarmService.class);

        final RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);

        return new TimerTask() {
            @Override
            public void run() {
                if(eqId!=null && !eqId.equals("")) {
                    EqAlarm eqAlarm = new EqAlarm();
                    eqAlarm.setEqId(eqId);
                    eqAlarm.setEqType(eqType);
                    eqAlarm.setAlarmType(alarmType);
                    eqAlarm.setAlarmMethod(alarmMethod);
                    eqAlarm.setAlarmMsg(alarmMsg);
                    eqAlarmService.insertEqAlarm(eqAlarm);

                    if(redisUtil.hasKey("error")){
                        redisUtil.incr("error", 1);
                    }
                }
            }
        };
    }
}
